package com.wyl.wyl.testapplication.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.wyl.wyl.testapplication.R;
import com.wyl.wyl.testapplication.bean.FunctionItem;
import com.wyl.wyl.testapplication.utils.ActivityUtils;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class FunctionListAdapter extends BaseAdapter {
    private static final int COLUMN_NUMBER = 3;
    private Activity mActivity;
    private List<FunctionItem> mFunctionList;
    private LayoutInflater inflater;

    public FunctionListAdapter(Activity activity, List<FunctionItem> functionList) {
        mActivity = activity;
        mFunctionList = functionList;
        inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return mFunctionList.size();
    }

    @Override
    public Object getItem(int position) {
        return mFunctionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FunctionItem functionItem = mFunctionList.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gridview_function_list, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mActivity, viewHolder, convertView, functionItem);
        //格式化样式
        formateFunctionStyle(viewHolder, position);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.picLayout = (RelativeLayout) view.findViewById(R.id.picLayout);
        viewHolder.pic = (ImageView) view.findViewById(R.id.pic);
        viewHolder.functionName = (TextView) view.findViewById(R.id.functionName);
        viewHolder.line = view.findViewById(R.id.line);
    }

    private void initData(Activity context, ViewHolder viewHolder, View view, FunctionItem functionItem) {
        viewHolder.pic.setImageResource(functionItem.getPic());
        viewHolder.functionName.setText(functionItem.getFunctionName());
        view.setOnClickListener((View v) -> {
            Intent intent = new Intent(context, functionItem.getClassName());
            if (functionItem.isForResult()) {
                mActivity.startActivityForResult(intent, ActivityUtils.REQUEST_CODE_SYSTEM_SETTING);
            } else {
                mActivity.startActivity(intent);
            }
        });
    }


    private void formateFunctionStyle(ViewHolder viewHolder, int position) {
        int column = getColumnNumber(position, COLUMN_NUMBER);
        switch (column) {
            case 1:
                RelativeLayout.LayoutParams leftParams = (RelativeLayout.LayoutParams) viewHolder.line.getLayoutParams();
                leftParams.addRule(RelativeLayout.ALIGN_LEFT, viewHolder.picLayout.getId());
                viewHolder.line.setLayoutParams(leftParams);
                break;
            case COLUMN_NUMBER:
                RelativeLayout.LayoutParams rightParams = (RelativeLayout.LayoutParams) viewHolder.line.getLayoutParams();
                rightParams.addRule(RelativeLayout.ALIGN_RIGHT, viewHolder.picLayout.getId());
                viewHolder.line.setLayoutParams(rightParams);
                break;
        }

        int row = getRowNumber(position, COLUMN_NUMBER);
        switch (row) {
            case 1:
//                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_white);
                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_blue);
                break;
            case 2:
                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_blue);
                break;
            case 3:
//                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_green);
                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_blue);
                break;
            case 4:
//                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_gray);
                viewHolder.picLayout.setBackgroundResource(R.drawable.function_bg_blue);
                break;
        }

        if (row == getMaxRowNumber(getCount(), COLUMN_NUMBER)) {
            viewHolder.line.setVisibility(View.INVISIBLE);
        }


    }

    private int getMaxRowNumber(int count, int columnNumber) {
        int maxRowNumber = 0;
        if (count % columnNumber != 0) {
            maxRowNumber++;
        }
        maxRowNumber += count / columnNumber;
        return maxRowNumber;
    }

    private int getColumnNumber(int position, int columnNumber) {
        return position % columnNumber + 1;
    }

    private int getRowNumber(int position, int columnNumber) {
        int result = 0;
        position++;
        if (position % columnNumber != 0) {
            result++;
        }
        result += position / columnNumber;
        return result;
    }

    private class ViewHolder {
        private ImageView pic;
        private TextView functionName;
        private RelativeLayout picLayout;
        private View line;
    }
}
